package controller.sistema_veiculos;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import persistencia.PersistenciaVeiculo;

import model.Veiculo;

@WebServlet("/ServletCadastrarVeiculos")
public class ServletCadastrarVeiculos extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Veiculo veiculo = requestToVeiculo(request);
		
		HttpSession session = request.getSession();
		session.setAttribute("veiculo", veiculo);
		
		PersistenciaVeiculo.inserirVeiculo(veiculo);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/sistema_veiculos/consultaVeiculo.jsp");
		dispatcher.forward(request, response);
	}

	private Veiculo requestToVeiculo(HttpServletRequest request) {
		String marca = request.getParameter("marca");
		String placa = request.getParameter("placa");
		String chassi = request.getParameter("chassi");
		String ano = request.getParameter("ano");
		String cor = request.getParameter("cor");
		
		Veiculo veiculo = new Veiculo(marca, "", placa, chassi, ano, cor);
		return veiculo;
	}
	
	
}
